from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys


class Base():
    # 初始化驱动driver
    def __init__(self,driver):
        self.driver = driver

    # 打开浏览器
    def open_url(self,url):
        self.driver.get(url)

    # 元素定位
    def find_element(self,loc):
        try:
            WebDriverWait(self.driver,20,0.5).until(lambda driver:driver.find_element(*loc).is_displayed)
            return self.driver.find_element(*loc)
        except Exception as e:
            print(f'元素定位不到{e}')

    # 点击
    def click(self,loc):
        self.find_element(loc).click()

    # 输入
    def send_keys(self,loc,info):
        self.find_element(loc).send_keys(info)

    # 悬停
    def move_to_element(self,loc):
        move = self.find_element(loc)
        ActionChains(self.driver).move_to_element(move).perform()

    # 全选
    def ctrl_a(self,loc):
        self.find_element(loc).send_keys(Keys.CONTROL,'a')

    # 复制
    def ctrl_c(self,loc):
        self.find_element(loc).send_keys(Keys.CONTROL,'c')

    # 粘贴
    def ctrl_v(self,loc):
        self.find_element(loc).send_keys(Keys.CONTROL,'v')

